from loguru import logger

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.school.processor.base_processor import BaseProcessor
from com.arcfox.util.muilty_coroutine_util import concurrency
from com.arcfox.util.redis_key_manager import YZY_UNIVERSITY_SCHOOL_LIST_KEY


class YZYProvinceScoreProcessor(BaseProcessor):


    async def parse_and_save_data(self, result_json):
        parse_result = []
        for item in result_json["result"]:
            batch_arr = item["batches"]
            for batch in batch_arr:
                parse_result.append({
                    "province_name": item["provinceName"],
                    "subject_item": batch["course"],
                    "score_year": batch["year"],
                    "score_level": batch["score"],
                    "batch_name": batch["batch"],
                    "line_range": batch["pressureRange"],
                })
        await self.__insert_province_score(tasks=parse_result)

    @concurrency(1)
    async def __insert_province_score(self, **params):
        task = params['task']
        mapping = db.MysqlMapping("tb_province_score")
        exist = await mapping.query(
            {
                "province_name": task["province_name"],
                "subject_item": task["subject_item"],
                "score_year": task["score_year"],
                "batch_name": task["batch_name"]
            }, ["id"])
        if exist:
            logger.info("已经存在{}", task)
            return
        await mapping.insert(task)
